package cn.xz.mxxz.activity

import android.os.Handler
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import cn.xz.mxxz.R
import cn.xz.mxxz.adapter.HycZhAdapter
import cn.xz.mxxz.base.BaseActivity
import cn.xz.mxxz.bean.*
import cn.xz.mxxz.databinding.ActivityWelcomeBinding
import cn.xz.mxxz.dialog.AddWelComeDialog
import cn.xz.mxxz.net.ControlDevice
import cn.xz.mxxz.net.OkHttpUtil
import cn.xz.mxxz.net.OkhttpResponseHandle
import cn.xz.mxxz.util.ToastUtil
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/**
 * 欢迎词界面*/
class WelcomeActivity: BaseActivity<ActivityWelcomeBinding>(R.layout.activity_welcome) {
    //标识符
    private var mTag = 0    //设备信息
    //设备信息
    private var mDevice: DeviceList? = null
    //列表适配器
    private var hycZhAdapter: HycZhAdapter? = null
    //当前欢迎词数据
    private val mList: ArrayList<Hyc> = arrayListOf()

    override fun initData() {
        //获取跳转数据
        mTag = intent.getIntExtra(CommonLib.JUMP_TYPE, -1)
        //获取当前设备信息
        if (mTag != -1){
            mDevice = ControlDevice.getThisDevice(mTag)
            if (CommonLib.MY_LOCAL_IP.isNotEmpty() && mDevice != null){
                mDevice!!.ip = CommonLib.MY_LOCAL_IP
            }
        }
        //如果数据异常直接退出
        if (mTag == -1 || mDevice == null){
            ToastUtil.show("当前数据异常")
            finish()
        }else{
            initView()
            //获取欢迎词列表 - 首先要让播放器显示出来
            OkHttpUtil.post("http://" + mDevice!!.ip + ":8020/?act=back&object=&states=2")
            Handler().postDelayed({
                getWelComeList()
            }, 500)
        }
    }

    private fun getWelComeList(){
        val url = "http://" + mDevice!!.ip + ":" + mDevice!!.port + "/?act=welcome&t=getxml&r=" + (Math.random() * 10000).toInt()
        OkHttpUtil.get(this@WelcomeActivity,
            url,
            HYCList::class.java, object : OkhttpResponseHandle<HYCList?>() {
                override fun onSuccess(response: HYCList?) {
                    if (response != null && response.list != null && response.list.isNotEmpty()){
                        mList.clear()
                        mList.addAll(response.list)
                        initRv()
                    }
                    //取消刷新
                    mDataBind.qtSwipe.isRefreshing = false
                }

                override fun onFailure(request: Request?, e: Exception?) {
                    super.onFailure(request, e)
                    //取消刷新
                    mDataBind.qtSwipe.isRefreshing = false
                }
            })
    }

    private fun initView(){
        mDataBind.qtSwipe.setColorSchemeResources(
            android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light
        )
        mDataBind.rvList.layoutManager = LinearLayoutManager(this)
        mDataBind.rvList.itemAnimator = DefaultItemAnimator()
    }

    private fun initRv(){
        if (hycZhAdapter != null){
            hycZhAdapter!!.upData(mList)
        }else{
            hycZhAdapter = HycZhAdapter(this@WelcomeActivity,
                object : HycZhAdapter.ZuohuaSlidingViewClickListener {
                    override fun onItemClick(view: View?, position: Int) {
                        //点击欢迎词
                        OkHttpUtil.post("http://" + mDevice!!.ip + ":" + mDevice!!.port + "/?act=welcome&t=openindex&object=" + position);
                    }

                    override fun onDelBtnCilck(view: View?, position: Int, bfList: List<Hyc?>?) {
                        //删除按钮
                        val url = "http://" + mDevice!!.ip + ":" + mDevice!!.port + "/?act=welcome&t=delete&id=" + position;
                        OkHttpUtil.get(url, object : Callback {
                            override fun onFailure(call: Call, e: IOException) {}

                            @Throws(IOException::class)
                            override fun onResponse(call: Call, response: Response) {
                                if (response.isSuccessful) {
                                    mDataBind.rvList.postDelayed({
                                        getWelComeList()
                                    },500)
                                }
                            }
                        })
                    }

                    override fun onEditBtnClick(view: View?, position: Int) {
                        //编辑按钮
                        AddWelComeDialog.newInstance(
                            mList[position].size,
                            mList[position].title.replace("|", "\n"),
                            object :AddWelComeDialog.OnClickListener{
                                override fun onYes(i: Int, s: String) {
                                    val url = "http://" + mDevice!!.ip + ":" + mDevice!!.port + "/?act=welcome&t=update&size=" + i + "&title=" + s + "&id=" + position
                                    OkHttpUtil.get(url, object : Callback {
                                        override fun onFailure(call: Call, e: IOException) {}

                                        @Throws(IOException::class)
                                        override fun onResponse(call: Call, response: Response) {
                                            mDataBind.rvList.post {
                                                mList[position].title = s
                                                mList[position].size = i
                                                hycZhAdapter!!.upData(mList)
                                            }
                                        }
                                    })
                                }
                            }
                        ).show(this@WelcomeActivity.supportFragmentManager,"AddWelComeDialog12")
                    }
                }, mList
            )
            mDataBind.rvList.adapter = hycZhAdapter
        }
    }

    override fun initEvent() {
        //返回
        mDataBind.tvBack.setOnClickListener {
            finish()
        }
        //添加
        mDataBind.ivAdd.setOnClickListener {
            //关闭抽屉
            hycZhAdapter?.closeMenu()
            AddWelComeDialog.newInstance(
                120,
                "请输入文字",
                object :AddWelComeDialog.OnClickListener{
                    override fun onYes(i: Int, s: String) {
                        val url = "http://" + mDevice!!.ip + ":" + mDevice!!.port + "/?act=welcome&t=add&size=" + i + "&title=" + s
                        OkHttpUtil.get(url, object : Callback {
                            override fun onFailure(call: Call, e: IOException) {}

                            @Throws(IOException::class)
                            override fun onResponse(call: Call, response: Response) {
                                getWelComeList()
                            }
                        })
                    }
                }
            ).show(this@WelcomeActivity.supportFragmentManager,"AddWelComeDialog12")
        }
        //列表循环
        mDataBind.ivX.setOnClickListener {
            //关闭抽屉
            hycZhAdapter?.closeMenu()
            //关闭选中
            hycZhAdapter?.closeSelect()
            OkHttpUtil.post("http://" + mDevice!!.ip + ":" + mDevice!!.port + "/?act=welcome&t=loop");
        }
        //下拉刷新
        mDataBind.qtSwipe.setOnRefreshListener {
            getWelComeList()
            //这里加个延迟，五秒之后判断是否已经取消刷新了，没有主动取消
            mDataBind.qtSwipe.postDelayed({
                if (mDataBind.qtSwipe.isRefreshing) {
                    mDataBind.qtSwipe.isRefreshing = false
                }
            },5 * 1000)
        }
    }


}